package com.acane.instagram.core.config

import androidx.compose.runtime.mutableStateListOf
import com.acane.instagram.core.model.MessageWrapper
import com.acane.instagram.core.model.Notification
import com.acane.instagram.core.model.NotifyType
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass

// NotificationManager.kt
object NotificationManager {
    private val _notifications = mutableStateListOf<Notification>()
    val notifications: List<Notification> get() = _notifications

    // 在公共模块创建序列化配置
    val notificationJson = Json {
        ignoreUnknownKeys = true
        classDiscriminator = "type" // JSON字段使用type作为鉴别器
        serializersModule = SerializersModule {
            polymorphic(Notification::class) {
                subclass(Notification.ChatMessage.serializer())
                subclass(Notification.LikeNotification.serializer())
                subclass(Notification.CommentNotification.serializer())
                subclass(Notification.CommentLikeNotification.serializer())
            }
        }
    }

    // 新增方法：解析 MessageWrapper
    fun parseMessageWrapper(json: String): Notification {
        val wrapper = notificationJson.decodeFromString<MessageWrapper<JsonElement>>(json) // 明确指定泛型类型
        return when (wrapper.type) {
            NotifyType.CHAT_MESSAGE -> notificationJson.decodeFromJsonElement(
                Notification.ChatMessage.serializer(),
                wrapper.data as JsonElement // 添加强制类型转换
            )
            NotifyType.POST_LIKE -> notificationJson.decodeFromJsonElement(
                Notification.LikeNotification.serializer(),
                wrapper.data as JsonElement
            )
            NotifyType.POST_COMMENT -> notificationJson.decodeFromJsonElement(
                Notification.CommentNotification.serializer(),
                wrapper.data as JsonElement
            )
            NotifyType.COMMENT_LIKE -> notificationJson.decodeFromJsonElement(
                Notification.CommentLikeNotification.serializer(),
                wrapper.data as JsonElement
            )
            NotifyType.FOLLOW_USER -> notificationJson.decodeFromJsonElement(
                Notification.FollowNotification.serializer(),
                wrapper.data as JsonElement
            )
            else -> throw IllegalArgumentException("未知的消息类型: ${wrapper.type}")
        }
    }

    fun handleNotification(notification: Notification) {
        when (notification) {
            is Notification.LikeNotification -> {
                //_notifications.add(0, notification)
                showLikeBadge()
            }
            is Notification.CommentNotification -> {
                _notifications.add(0, notification)
                showCommentBadge()
            }
            is Notification.ChatMessage -> {} // 由ChatViewModel处理
            is Notification.FollowNotification -> {
                _notifications.add(0, notification)
            }

            is Notification.CommentLikeNotification -> {
                //_notifications.add(0, notification)
            }
        }
    }

    private fun showLikeBadge() {
        // 更新点赞红点状态
    }

    private fun showCommentBadge() {
        // 更新评论红点状态
    }
}
